package abc.com.abc.view;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Map;

import abc.com.abc.R;
import abc.com.abc.activity.LoginActivity;
import abc.com.abc.activity.SettingActivity;
import abc.com.abc.activity.UserInfoActivity;
import abc.com.abc.utils.AnalysisiUtils;
import abc.com.abc.utils.MD5Utils;

import static android.content.Context.MODE_PRIVATE;

public class MyInfoView {
    /**
     *
     * @param context
     */
    public ImageView iv_head_icon;
    private LinearLayout ll_head;
    private RelativeLayout rl_myinfo_history,rl_setting;
    private TextView tv_user_name,tv_myname;
    private Activity mContext;
    private LayoutInflater mInflater;
    private View mCurrentView;

    public MyInfoView(Activity context){
        mContext = context;
        mInflater = LayoutInflater.from(mContext);
    }
    private  void createView(){

        initView();
    }

    /**
     * 获取界面控件
     */
    private void initView() {
        mCurrentView = mInflater.inflate(R.layout.main_view_myinfo,null);
        ll_head = mCurrentView.findViewById(R.id.ll_head);
        iv_head_icon = mCurrentView.findViewById(R.id.iv_head_icon);
        rl_myinfo_history =mCurrentView.findViewById(R.id.rl_MyInfo_history);
        rl_setting=mCurrentView.findViewById(R.id.rl_setting);
        tv_myname = mCurrentView.findViewById(R.id.tv_myname);
        tv_user_name = mCurrentView.findViewById(R.id.tv_user_name);
        mCurrentView.setVisibility(View.VISIBLE);

        setLoginParams(readLoginStatus());//设置登录时界面控制的状态
        ll_head.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //判断是否的登陆
                if (readLoginStatus()){
                    //登陆好跳转到个人资料界面
                    Intent intent = new Intent(mContext, UserInfoActivity.class);
                    mContext.startActivity(intent);
                }else {
                    //没有登陆跳转到登陆界面
                    Intent intent = new Intent(mContext, LoginActivity.class);
                    mContext.startActivityForResult(intent,1);
                }
            }
        });

        rl_myinfo_history.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (readLoginStatus()){
                    //跳转到播放页面url
                    Uri uri = Uri.parse("https://hn.122.gov.cn/m/login");
                    Intent intent = new Intent(Intent.ACTION_VIEW,uri );
                    mContext.startActivity(intent);
                }else {
                    Toast.makeText(mContext,"请先登陆",Toast.LENGTH_SHORT).show();

                }
            }
        });

        rl_setting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (readLoginStatus()){
                    //跳转到设置页面
                    Intent intent = new Intent(mContext, SettingActivity.class);
                    mContext.startActivityForResult(intent,1);

                }else {
                    Toast.makeText(mContext,"请先登录",Toast.LENGTH_SHORT).show();

                }
            }
        });

    }




    /**
     * 登陆成功后设置我的页面
     */
    public void setLoginParams(boolean isLogin) {
        if (isLogin){
            tv_user_name.setText(AnalysisiUtils.readLoginUserName(mContext));
            tv_myname.setText("欢迎使用驾考指南！");
        }else {
            tv_user_name.setText("点击登陆");
            tv_myname.setText("");
        }
    }
    /**
     * 获取当前z导航栏上方显示的View
     */
    public View getView(){
        if (mCurrentView == null){
            createView();
        }
        return mCurrentView;
    }

    /**
     * 显示当前导航栏上方所对应的VIEW界面
     */
    public  void showView(){
        if (mCurrentView == null){
            createView();
        }
        mCurrentView.setVisibility(View.VISIBLE);
    }

    /**
     * 从SharedPreferences 中读取登陆状态
     */
    private boolean readLoginStatus() {
        //loginInfo表示文件名
        SharedPreferences sp = mContext.getSharedPreferences("loginInfo",MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();//获取编辑器
        Map<String,?> param = sp.getAll();
        for (String s : param.keySet()) {
            Log.i("readLoginStatus","key = " + s + "");
            Log.i("readLoginStatus","value = " + param.get(s) + "");
        }

        boolean isLogin = sp.getBoolean("isLogin",false);
        return  isLogin;

    }
}
